#include <iostream>
#include <stdlib.h>
#include <fstream>
#include "list.h"
#include <cmath>

using namespace std;
const int HashCount=20001;

int HashFunction(char *word){
	unsigned int H=0;
	for(int i=0; i<strlen(word); i++)
		H+=word[i]*(powl(i+1,strlen(word)-i));
	if(H==0){

	}
		//H+=word[i];
	return H%HashCount;
}

void main(){
	int HashCode=0, WordsCount=0;
	char *word;
	word = new char[100];
	
	word[0]=' ';
	ifstream infile;
	do {
		if(word[0]!=' ')
			cout << endl << "There are not file with this name." << endl;
		cout << "Files Name : ";
		cin >> word;
		cout << endl;
		infile.open(word);
	}while(infile.is_open()==0);
	
	Node *Hash[HashCount-1];
	for(int i=0;i<HashCount;i++)
		Hash[i] = new Node;

	while(infile.peek()!=-1){
		WordsCount++;
		infile >> word;
		HashCode=HashFunction(word);
		if(Hash[HashCode]->count!=0){
			add_end(Hash[HashCode],word);
		}
		else{
			strcpy(Hash[HashCode]->data = new char[strlen(word)],word); 
			(Hash[HashCode]->count=1);
		}
	}
	print_Hash(Hash,HashCount);
	cout << endl << "There are "<< WordsCount << " in this text. "<< endl;
	word[0] = 'y';
	int i=0;

	while(word[0]!='n'){
		cout << endl << "What word do search for you"<< endl;
		cin >> word;
		for(i=0;i<HashCount;i++)
			search_word(Hash[i],word);

		if(i>HashCount) cout << endl << " There is no Word <" << word <<"> in this text.";
		cout << endl << "Contin? y/n"<< endl;
		cin >> word;
	}
	
	
	del_all(Hash,HashCount);
	system("pause");
}